package com.sanerzone.smscenter.modules.sms.task;

import com.sanerzone.jeebase.common.utils.DateUtils;
import com.sanerzone.jeebase.common.utils.JedisUtils;
import com.sanerzone.smscenter.common.utils.AccountCacheUtils;
import com.sanerzone.smscenter.modules.account.entity.BaseAccount;
import com.sanerzone.smscenter.modules.account.service.BaseAccountService;
import com.sanerzone.smscenter.modules.sms.service.SmsDayReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Lazy(false)
public class SmsDayAccountClearTask {
	
	private static Logger logger = LoggerFactory.getLogger(SmsDayAccountClearTask.class);
	
	@Autowired
	private BaseAccountService baseAccountService;
	
	@Autowired
	private SmsDayReportService smsDayReportService;

	@Autowired
	private BaseAccountService jmsgAccountService;
	//每天 00:10:30执行日账务清算任务

	@Scheduled(cron = "30 10 0 * * ?")
	@Transactional(readOnly = false)
	public void run(){
		try{
			int day = -1;
			
			String tableName = "sms_send_"+DateUtils.getDayOfMonth(day);
			Date dateYesterday=DateUtils.getDay( -1 );
			smsDayReportService.saveSmsDayReport(false, tableName, dateYesterday);//统计昨天的发送报表

			List<BaseAccount> list = jmsgAccountService.findList(new BaseAccount());
			if(list != null && list.size() > 0){
				for (BaseAccount entity : list) {
					String userid = entity.getUserid();
					String accId = entity.getId();
					String key = AccountCacheUtils.getAmountKey("sms", userid);
					String theUserAmount=JedisUtils.get( key );//得到在缓存中的余额  同步到数据库中
					int res=0;
					try {
						//缓存中余额
						int userAmount=Integer.parseInt( theUserAmount );
						//数据库中余额
						int left=baseAccountService.findUserMoeny(userid).intValue();
						res=userAmount-left;
						int code = baseAccountService.rechargeAmount(accId, userid, "XF02", String.valueOf(res), "自消费操作(消费日期："+DateUtils.formatDate(DateUtils.getDay(day), "yyyy-MM-dd") +")", userid);
					}catch (Exception e){
						logger.info( "清算任务用户id为{}的数据格式化出错",userid );
						continue;
					}

				}

			}
//			if(list != null && list.size() > 0){
			//				tableName = "sms_send_"+DateUtils.getDayOfMonth(-1);//获取当前发送表
			//				param = new SmsDayReport();
			//				param.setTableName(tableName);
			//				//代表的是每个用户
			//				for (SmsDayReport entity : list) {
			//					String userid = entity.getUserid();
			//					String accId = entity.getAccId();
			//					param.setUserid(userid);
			//					param.setAccId(accId);
			//					List<SmsDayReport> smsSendList = smsDayReportService.findSendList(param);//从sms_send_xx表中获取用户日发送量
			//					int todayCount = 0;
			//					if(smsSendList != null && smsSendList.size() > 0){//统计这个用户昨天发送
			//						for (SmsDayReport smsSend : smsSendList) {
			//							todayCount+=smsSend.getSendCount();
			//						}
			//					}
			//					String key = AccountCacheUtils.getAmountKey("sms", userid);
			//					//这里只要发多少，就扣
			//					int left=baseAccountService.findUserMoeny(userid).intValue();
			//					int amount=left-todayCount;
			//					int code = baseAccountService.rechargeAmount(accId, userid, "XF02", String.valueOf(-todayCount), "自消费操作(消费日期："+DateUtils.formatDate(DateUtils.getDay(day), "yyyy-MM-dd") +")", userid);
			//					if(code == 1)JedisUtils.set(key, String.valueOf(amount), 0);//置值
			//				}
			//
			//			}
			
		}catch(Exception e){
			logger.error("定时任务,执行日账务清算任务失败",e);
		}
		
		
	}
	
}
